/*******************************************************************************
 * @brief: 参数设置
 * @Author:   aple
 * @Date: 2020-09-02 23:35:00
 * @LastEditors: aple
 * @LastEditTime: 2022-11-12 08:43:00
 * @Verision: 1.0.0.0
 ******************************************************************************/
#include "par_jump.h"

/*******************************************************************************
 * @brief  FUN
 *******************************************************************************/
int32_t par_opt_val_get(par_manager_type *par_man, uint16_t par_num);

/*******************************************************************************
 * @brief  par_jump_next
 * \param[in] par_man
 * \retval: par_num,
 *******************************************************************************/
uint16_t par_jump_next(par_manager_type *par_man)
{
	uint16_t index;
	uint16_t par_attr;
	int16_t par_num;
	uint8_t st_jump;

	if (par_man->par_val_old.ival != par_man->par_val.ival)
		par_man->st_val_changed = 1;

	par_num = par_man->par_num;

	//获取当前参数属性
	par_attr = par_man->attr_tab[par_num].attr;

	//搜索下一个
	while (1)
	{
		//最后一个参数，退出
		if (par_attr & PAR_ATTR_END)
		{
			par_num = 0;
			par_attr = 0;
			break;
		}

		//参数强制跳转
		st_jump = 0;
		if (par_man->jump_list)
		{
			for (index = 0;; index++) //强制跳转搜索
			{
				if (0 == par_man->jump_list[index].src_pos)
				{
					break;
				}

				if (par_num == par_man->jump_list[index].src_pos)
				{
					st_jump = 1;
					par_num = par_man->jump_list[index].des_pos;
					break;
				}
			}
		}

		//如果没有强制跳转，参数索引递增
		if (!st_jump)
		{
			if (par_attr & PAR_ATTR_32BIT)
			{
				par_num++;
			}

			par_num++;
		}

		par_attr = par_man->attr_tab[par_num].attr;

		//无效参数，跳过，搜索下一个参数
		if (PAR_ATTR_NFUN == (par_attr & PAR_ATTR_NFUN))
		{
			continue;
		}

		//有效数据参数,结束
		break;
	}

	par_man->par_num = par_num;
	par_man->par_attr = par_attr;
	par_man->par_val.ival = par_opt_val_get(par_man, par_num);
	par_man->par_val_old.ival = par_man->par_val.ival;

	return par_num;
}

/*******************************************************************************
 * @brief  par_jump_prev
 * \param[in] par_man
 * \retval: par_num
 *******************************************************************************/
uint16_t par_jump_prev(par_manager_type *par_man)
{
	uint16_t index;
	uint16_t par_attr;
	int16_t par_num;
	uint8_t st_jump;

	if (par_man->par_val_old.ival != par_man->par_val.ival)
		par_man->st_val_changed = 1;

	par_num = par_man->par_num;
	//获取当前参数属性
	par_attr = par_man->attr_tab[par_num].attr;

	//搜索下一个
	while (1)
	{
		if (par_num == 0)
		{
			break;
		}

		// 前移到头部退出
		if (par_num == par_man->start_num)
		{
			break;
		}

		//参数强制跳转
		st_jump = 0;
		if (par_man->jump_list)
		{
			for (index = 0;; index++) //强制跳转搜索
			{
				if (0 == par_man->jump_list[index].src_pos)
				{
					break;
				}

				if (par_num == par_man->jump_list[index].des_pos)
				{
					st_jump = 1;
					par_num = par_man->jump_list[index].src_pos;
					break;
				}
			}
		}

		//如果没有强制跳转，参数索引递增
		if (!st_jump)
		{
			par_num--;
			par_attr = par_man->attr_tab[par_num].attr;

			if (par_attr & PAR_ATTR_32BIT)
			{
				par_num--;
			}
		}
		else
		{
			par_attr = par_man->attr_tab[par_num].attr;
		}

		//无效参数，跳过，搜索下一个参数
		if (PAR_ATTR_NFUN == (par_attr & PAR_ATTR_NFUN))
		{
			continue;
		}

		//有效数据参数,结束
		break;
	}

	par_man->par_num = par_num;
	par_man->par_attr = par_attr;
	par_man->par_val.ival = par_opt_val_get(par_man, par_num);
	par_man->par_val_old.ival = par_man->par_val.ival;

	return par_num;
}

/*******************************************************************************
 * @brief  par_jump_set
 * \param[in] par_man, par_num
 * \retval: par_num
 *******************************************************************************/
uint16_t par_jump_set(par_manager_type *par_man, int16_t par_num)
{
	par_man->par_num = par_num;
	par_man->par_attr = par_man->attr_tab[par_num].attr;
	par_man->par_val.ival = par_opt_val_get(par_man, par_num);
	par_man->par_val_old.ival = par_man->par_val.ival;

	return par_num;
}

/*******************************************************************************
 * @brief  par_jump_start
 * \param[in] par_man, par_num
 * \retval: par_num
 *******************************************************************************/
uint16_t par_jump_start(par_manager_type *par_man, int16_t par_num)
{
	par_jump_set(par_man, par_num);

	par_man->start_num = par_num;

	par_man->st_val_changed = 0;

	return par_num;
}

/*******************************************************************************
 * @brief  group sel
 * \param[in] par_man, par_num
 * \retval: par_num
 *******************************************************************************/
void par_jump_init(par_manager_type *par_man, u16_tt *par_sel, par_attr_type *attr_sel, uint16_t amount)
{
	par_man->tab = par_sel;
	par_man->attr_tab = attr_sel;
	par_man->par_amount = amount;
}

/*******************************************************************************
 * @brief  par value get
 * \param[in] par_num : par index,
 * \retval: value
 *******************************************************************************/
int32_t par_opt_val_get(par_manager_type *par_man, uint16_t par_num)
{
	dword_tt val;

	if (par_num > par_man->par_amount)
		return 0;

	if (par_man->attr_tab[par_num].attr & PAR_ATTR_32BIT)
	{
		val.uVal = par_man->tab[par_num].uval + (par_man->tab[par_num + 0x01].uval << 16);
	}
	else
	{
		val.iVal = par_man->tab[par_num].ival;
	}

	return val.iVal;
}
